package moodish.dummy.junit;

import static org.junit.Assert.*;

import moodish.client.MoodishClient;
import moodish.dummy.ClientCommDummy;
import moodish.dummy.MoodishClientDummy;
import moodish.dummy.MoodishServerDummy;
import moodish.dummy.ServerCommDummy;
import moodish.server.MoodishServer;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class ServerAndClient {

	private static ServerCommDummy serverComm;
	private static ClientCommDummy clientComm;
	
	@BeforeClass 
	public static void before() throws InterruptedException{
		// inicia o servidor
		serverComm = new ServerCommDummy();
		MoodishServer server = new MoodishServerDummy();
		server.start(serverComm);
		
		Thread.sleep(500);
		
		// criar dois servidores, para dar erro
		ServerCommDummy serverComm2 = new ServerCommDummy();
		MoodishServer server2 = new MoodishServerDummy();
		server2.start(serverComm2);
		
		Thread.sleep(500);
		
		// inicia o cliente
		clientComm = new ClientCommDummy();
		MoodishClient client = new MoodishClientDummy();
		client.start(clientComm);
		
		Thread.sleep(500);
		
		// para testar que nao aceita dois clientes com o mesmo nome.
		ClientCommDummy clientComm2 = new ClientCommDummy();
		MoodishClient client2 = new MoodishClientDummy();
		client2.start(clientComm2);
		
		Thread.sleep(500);
		//////////////////////////
		// cliente envia mensagens
		clientComm.sendMoodishMessage("ola");
		clientComm.friendship("lock");
		
		// servidor envia mensagens
		serverComm.sendMoodishMessage("lock", "teste", "ola, tudo bem?");
		serverComm.sendError("teste", "erro");
		serverComm.sendClientDisconnected("teste", "lock");
		serverComm.sendClientConnected("teste", "lock");
		serverComm.sendNewFriendship("teste", "lock");
		
	}
	
	@Test
	public void serverGetMessage() throws InterruptedException {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// fica em espera quando acabar as mensagens
				while(true){
					serverComm.getNextMessage();
				}
				
			}
		}).start();
		// para que o servidor fique em espera e depois ir buscar a mensagem
		Thread.sleep(500);
		clientComm.unfriendship("lock");
	}
	
	@Test
	public void clientGetMessage() throws InterruptedException {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// fica em espera quando acabar as mensagens
				while(true){
					clientComm.getNextMessage();
				}
				
			}
		}).start();
		// para que o cliente fique em espera e depois ir buscar a mensagem
		Thread.sleep(500);
		serverComm.sendNewUnfriendship("teste", "lock");
	}
	
	@Test
	public void isConnectedTrue(){
		assertTrue(clientComm.isConnected());
		assertTrue(serverComm.clientIsConnected("teste"));
	}
	
	@Test
	public void isConnectedFalse(){
		assertFalse(serverComm.clientIsConnected("lock"));
	}

	@AfterClass 
	public static void after(){
		clientComm.disconnect();
		serverComm.disconnectClient("teste");
		
		assertFalse(clientComm.isConnected());
		
		// ligacao terminada, deve retornar null
		assertNull(clientComm.getNextMessage());
		
	}
	

}
